package com.shameit.goodqrinfomanager.ui.activity.barcode;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProviders;

import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.SpinnerAdapter;

import com.codbking.widget.DatePickDialog;
import com.codbking.widget.OnSureLisener;
import com.codbking.widget.bean.DateType;
import com.shameit.goodqrinfomanager.AppViewModelFactory;
import com.shameit.goodqrinfomanager.BR;
import com.shameit.goodqrinfomanager.BaseMVVMActivity;
import com.shameit.goodqrinfomanager.MyApplication;
import com.shameit.goodqrinfomanager.R;
import com.shameit.goodqrinfomanager.bean.litepal.CangKu;
import com.shameit.goodqrinfomanager.bean.litepal.CangWei;
import com.shameit.goodqrinfomanager.databinding.ActivityBarCodeManagementBinding;
import com.shameit.goodqrinfomanager.ui.activity.login.LoginViewModel;
import com.shameit.goodqrinfomanager.ui.activity.offline.OfflineManagementActivity;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

import java.util.Date;
import java.util.List;

import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class BarCodeManagementActivity extends BaseMVVMActivity<ActivityBarCodeManagementBinding,BarCodeViewModel> {
    public final static String danCode = "danCode";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolBarBack(binding.toolbar);
        getLifecycle().addObserver(new BarCodeViewModel(getApplication()));

    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_bar_code_management;
    }

    @Override
    public int initVariableId() {
        return BR.data;
    }

    @Override
    public BarCodeViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(BarCodeViewModel.class);
    }

    @Override
    protected String setTitle() {
        int dan = SPUtils.getInstance().getInt(danCode,10000);
        return String.valueOf(dan);
    }

    @Override
    protected void initView() {
        binding.etXm.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    viewModel.hasFocus.set(true);
                } else {
                    viewModel.hasFocus.set(false);
                }
            }
        });
        binding.toolbar.setRefesh("新单");
        binding.toolbar.newOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int dan = SPUtils.getInstance().getInt(danCode,10000);
                dan++;
                SPUtils.getInstance().put(danCode,dan);

                //ToastUtils.showLong("你点击了新单");
                finish();
                startActivity(getIntent());
            }
        });
        binding.barCodeShow.setMovementMethod(ScrollingMovementMethod.getInstance());
        binding.barCodeShow.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                binding.barCodeShow.setSelection(binding.barCodeShow.getText().length(),binding.barCodeShow.getText().length());
            }
        });
//        binding.tvLxgl.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent it = new Intent(BarCodeManagementActivity.this, OfflineManagementActivity.class);
//                startActivity(it);
//            }
//        });
        binding.etScrq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickDialog dialog = new DatePickDialog(BarCodeManagementActivity.this);
                //设置上下年分限制
                dialog.setYearLimt(5);
                //设置标题
                dialog.setTitle("选择时间");
                //设置类型
                dialog.setType(DateType.TYPE_YMDHM);
                //设置消息体的显示格式，日期格式
                dialog.setMessageFormat("yyyy-MM-dd HH:mm");
                //设置选择回调
                dialog.setOnChangeLisener(null);
                //设置点击确定按钮回调
                dialog.setOnSureLisener(new OnSureLisener() {
                    @Override
                    public void onSure(Date dates) {
                        binding.etScrq.setText(MyApplication.yyHHMMddhhmmss.format(dates));
                        viewModel.scrq.set(MyApplication.yyHHMMddhhmmss.format(dates));
                    }
                });
                dialog.show();
            }
        });
    }

    @Override
    protected void loadData() {
        viewModel.scph.set(SPUtils.getInstance().getInt(danCode,10000)+"");
        String[] arr = viewModel.loadCangkuItem();
        ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item, arr);
        binding.spCkbh.setAdapter(arrayAdapter);

        binding.spCkbh.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                binding.spCwbh.setAdapter(null);
                binding.spCpbh.setAdapter(null);
                String thisCangku = arr[position];
                binding.spCwbh.setAdapter(new ArrayAdapter(BarCodeManagementActivity.this, R.layout.support_simple_spinner_dropdown_item, viewModel.loadCangweiItem(thisCangku)));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        if (arr.length>0){
            String thisCangkuNo = binding.spCkbh.getSelectedItem().toString();
            arrayAdapter = new ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item, viewModel.loadCangweiItem(thisCangkuNo));
            binding.spCwbh.setAdapter(arrayAdapter);

            binding.spCwbh.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    viewModel.cwbh.set(binding.spCwbh.getSelectedItem().toString());
                    String[] cpcode = viewModel.loadChanpinItem();
                    ArrayAdapter chanpinAdapter = new ArrayAdapter(BarCodeManagementActivity.this, R.layout.support_simple_spinner_dropdown_item, cpcode);
                    binding.spCpbh.setAdapter(chanpinAdapter);
                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {

                }
            });
        }
        binding.spCpbh.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String code = binding.spCpbh.getSelectedItem().toString();
                viewModel.cpbh.set(code);
                String name = viewModel.loadChanpinName(code);
                viewModel.cpmc.set(name);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }
}